package com.sxw.hw_student.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxw.hw_student.system.entity.Sysuser;
import com.sxw.hw_student.system.entity.SysUserRole;
import com.sxw.hw_student.system.mapper.SysUserRoleMapper;
import com.sxw.hw_student.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    // 注册接口
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody Sysuser sysUser) {
        Map<String, Object> map = new HashMap<>();
        Sysuser existingUser = sysUserService.findByName(sysUser.getUserName());
        if (existingUser != null) {
            map.put("success", false);
            map.put("message", "用户名已存在");
            return map;
        }
        sysUserService.save(sysUser);
        map.put("success", true);
        map.put("message", "注册成功");
        return map;
    }

    // 登录接口
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Sysuser sysUser) {
        Map<String, Object> response = new HashMap<>();

        Sysuser user = sysUserService.findByName(sysUser.getUserName());

        if (user == null) {
            response.put("success", false);
            response.put("message", "用户名不存在");
            return response;
        }

        if (!user.getPassword().equals(sysUser.getPassword())) {
            response.put("success", false);
            response.put("message", "密码错误");
            return response;
        }

        SysUserRole userRole = sysUserRoleMapper.selectOne(
                new QueryWrapper<SysUserRole>().eq("user_id", user.getUserId())
        );

        Long roleId = userRole != null ? userRole.getRoleId() : null;

        response.put("success", true);
        response.put("message", "登录成功");
        response.put("roleId", roleId);
        response.put("userName", user.getUserName());
        response.put("userId", user.getUserId()); // 可选：为后续扩展
        return response;
    }

    // 查询所有用户
    @GetMapping
    public List<Sysuser> getAllUsers() {
        return sysUserService.list();
    }

    // 忘记密码（重置密码）接口
    @PostMapping("/resetPassword")
    public Map<String, Object> resetPassword(@RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();

        String userName = params.get("userName");
        String pid = params.get("pid");
        String email = params.get("email");
        String newPassword = params.get("newPassword");

        QueryWrapper<Sysuser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName)
                .eq("pid", pid)
                .eq("email", email);

        Sysuser user = sysUserService.getOne(wrapper);

        if (user == null) {
            result.put("success", false);
            result.put("message", "身份信息验证失败，请检查输入是否正确");
        } else {
            user.setPassword(newPassword);
            sysUserService.updateById(user);
            result.put("success", true);
            result.put("message", "密码重置成功");
        }

        return result;
    }
}
